package com.stms.servlet;

import com.stms.bean.Student;
import com.stms.dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateStudent")
public class updateStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        // 获取表单数据
        String stuName = req.getParameter("stuName");
        String className = req.getParameter("className");
        String stuNo = req.getParameter("stuNo");
        String sex = req.getParameter("sex");
        Integer age = Integer.parseInt(req.getParameter("age"));
        String addr = req.getParameter("addr");

        Student student = new Student(id,stuName,className,stuNo,sex,age,addr);
        // 调用 DAO 层进行更新
        StudentDao studentDao = new StudentDao();
        int result = studentDao.updateStudent(student);
        // 结果重定向页面
        if (result > 0) {
            resp.sendRedirect("success.jsp");
        } else {
            resp.sendRedirect("failure.jsp");
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
